package com.zksk.admin.controller.org;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zksk.admin.domain.agreement.vo.AgreementVo;
import com.zksk.admin.domain.org.dto.SignDto;
import com.zksk.admin.domain.org.vo.FieldRequestVo;
import com.zksk.admin.service.org.FieldSelectService;
import com.zksk.admin.service.org.ROrgProdFieldService;
import com.zksk.common.core.constant.OrgConstants;
import com.zksk.common.core.domain.R;
import com.zksk.common.core.web.controller.BaseController;
import com.zksk.common.log.annotation.Log;
import com.zksk.common.log.enums.BusinessType;
import com.zksk.mbg.admin.domain.FieldSelect;
import com.zksk.mbg.admin.domain.OrgProduct;
import com.zksk.mbg.admin.domain.ROrgProdField;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Insert;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.websocket.server.PathParam;
import java.util.*;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/field")
@Api(value = "FieldController", tags = "字段相关接口")
public class FieldController extends BaseController {

    @Autowired
    private FieldSelectService fieldService;

    @Autowired
    private ROrgProdFieldService rOrgProdFieldService;

    /**
     * 字段信息列表 标段与协议
     * @return
     */
    @Log(title = "信息字段查询，统一返回，前端自主进行数据获取", businessType = BusinessType.SELECT)
    @ApiOperation(value = "信息字段查询")
    @GetMapping
    public R<Map<String, List<FieldSelect>>> fieldList(){
        Map<String, List<FieldSelect>> fieldMap = new HashMap<>();

        List<FieldSelect> companyFieldList = fieldService.list(
                new QueryWrapper<FieldSelect>()
                        .eq(FieldSelect.FIELD_TYPE, OrgConstants.ORG_TABLE_TYPE_COMPANY)
                        .eq(FieldSelect.HIDDEN_FLAG, OrgConstants.FIELD_SELECT_HIDDEN_1) // 非隐藏
                .orderByAsc(FieldSelect.ORDER_NUM)
        );

        List<FieldSelect> sectionFieldList = fieldService.list(
                new QueryWrapper<FieldSelect>()
                        .eq(FieldSelect.FIELD_TYPE, OrgConstants.ORG_TABLE_TYPE_SECTION)
                        .eq(FieldSelect.HIDDEN_FLAG, OrgConstants.FIELD_SELECT_HIDDEN_1) // 非隐藏
                        .orderByAsc(FieldSelect.ORDER_NUM)
        );

        fieldMap.put(OrgConstants.ORG_TABLE_TYPE_COMPANY, companyFieldList);
        fieldMap.put(OrgConstants.ORG_TABLE_TYPE_SECTION, sectionFieldList);

        return R.ok(fieldMap);
    }

    /**
     * company企业信息字段
     * @return
     */
    @Log(title = "企业信息字段查询", businessType = BusinessType.SELECT)
    @ApiOperation(value = "企业信息字段查询")
    @GetMapping("/company")
    public R<List<FieldSelect>> companyList(){
        List<FieldSelect> fieldList = fieldService.list(
                new QueryWrapper<FieldSelect>()
                        .eq(FieldSelect.FIELD_TYPE, OrgConstants.ORG_TABLE_TYPE_COMPANY)
                        .eq(FieldSelect.HIDDEN_FLAG, OrgConstants.FIELD_SELECT_HIDDEN_1) // 非隐藏
                        .orderByAsc(FieldSelect.ORDER_NUM)
        );

        return R.ok(fieldList);
    }


    /**
     * section标段信息字段
     * @return
     */
    @Log(title = "标段信息字段列表查询", businessType = BusinessType.SELECT)
    @ApiOperation(value = "标段信息字段列表查询")
    @GetMapping("/section")
    public R<List<FieldSelect>> sectionList(){
        List<FieldSelect> fieldList = fieldService.list(
                new QueryWrapper<FieldSelect>()
                        .eq(FieldSelect.FIELD_TYPE, OrgConstants.ORG_TABLE_TYPE_SECTION)
                        .eq(FieldSelect.HIDDEN_FLAG, OrgConstants.FIELD_SELECT_HIDDEN_1) // 非隐藏
                        .orderByAsc(FieldSelect.ORDER_NUM)
        );

        return R.ok(fieldList);
    }

    @Log(title = "新增字段(type=1 section|2 company)", businessType = BusinessType.INSERT)
    @ApiOperation(value = "新增字段")
    @PostMapping("/{type}")
    public R<FieldSelect> insert(@PathVariable("type") Integer type,
                                @Validated(Insert.class) @RequestBody FieldRequestVo fieldRequestVo){

        FieldSelect fieldSelect = new FieldSelect();
        BeanUtils.copyProperties(fieldRequestVo, fieldSelect);
        if (fieldRequestVo.getDefaultFlag() == null) {
            fieldRequestVo.setDefaultFlag(OrgConstants.FIELD_SELECT_DEFAULT_1); // 非默认
        }
        if (fieldRequestVo.getHiddenFlag() == null) {
            fieldRequestVo.setHiddenFlag(OrgConstants.FIELD_SELECT_HIDDEN_1); // 显示
        }
        if (type == 1) {
            fieldSelect.setFieldType(OrgConstants.ORG_TABLE_TYPE_COMPANY);
        }else if (type == 2) {
            fieldSelect.setFieldType(OrgConstants.ORG_TABLE_TYPE_SECTION);
        }else{
            return R.fail("类型错误，新增字段失败");
        }

        fieldService.save(fieldSelect);
        return R.ok(fieldSelect, "保存字段信息成功");
    }


    @Log(title = "批量删除字段", businessType = BusinessType.DELETE)
    @ApiOperation(value = "批量删除字段")
    @DeleteMapping("/{ids}")
    public R<String> delete(@PathVariable("ids") List<Long> ids){
        // 删除字段
        fieldService.removeBatchByIds(ids);
        return R.ok("删除成功");
    }

    /**
     * 根据传入orgProductId 进行查询
      * @return
     */
    @Log(title = "根据传入得机构产品id获取列表数据", businessType = BusinessType.SELECT)
    @ApiOperation(value = "根据传入得机构产品id获取列表数据")
    @GetMapping("/getListByOrgProductId")
    public R<List<FieldSelect>> listByOrgProductId(@RequestParam("orgProductId") Long orgProductId){

        List<ROrgProdField> list = rOrgProdFieldService.list(
                new QueryWrapper<ROrgProdField>()
                        .eq(ROrgProdField.ORG_PRODUCT_ID, orgProductId)
        );
        List<Long> collect = Optional.ofNullable(list).orElseGet(Collections::emptyList)
                .stream().map(ROrgProdField::getFieldId).collect(Collectors.toList());

        List<FieldSelect> fieldSelects = fieldService.list(
                new QueryWrapper<FieldSelect>().in(FieldSelect.FIELD_ID, collect));
        return R.ok(fieldSelects, "获取列表数据");
    }


}
